package switchviz.network;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;

import switchviz.core.ResourceManager;
import switchviz.gui.component.JLabeldComponent;
import switchviz.gui.component.JMaskedTextField;
import switchviz.gui.component.PaddingPanel;
import switchviz.gui.component.TestNetworkOptionPane;
import switchviz.network.netswitch.SwitchDataModel;
import switchviz.network.snmp.SnmpStatus;

public class NetworkStatePanel extends PaddingPanel implements ActionListener
{

	/**
	 * 
	 */
	private static final long serialVersionUID = 7495586724462179019L;
	private JLabel _lblname = null;
	private JLabeldComponent _lbluptime = null;
	private JLabeldComponent _lbldata = null;
	private JLabeldComponent _lblreceive = null;
	private JLabeldComponent _lblsend = null;
	private JLabeldComponent _lbltimedout = null;
	private JMaskedTextField _txttimerintervall = null;
	//private JLabeldComponent _cbsynchron = null;
	private JButton _butttime = null;
	private JButton _butttest = null;
	private SwitchDataModel _sdm = null;
	
	public NetworkStatePanel(SwitchDataModel sdm)
	{
		_sdm = sdm;
		initialize();
		_lblname.setText(_sdm.getName() + " @ " + _sdm.getAddress() + ":" +
						 _sdm.getPort());
		
		setUpTime(_sdm.getUpTime());
	}
	
	private void setUpTime(long time)
	{
	    int seconds = (int)((time/100) % 60);
	    int minutes = (int)((time/6000) % 60);
	    int hours = (int)((time/360000) % 24);
	    int days = (int)(time/8640000);
	    
	    String secondsStr = (seconds<10 ? "0" : "")+ seconds;
	    String minutesStr = (minutes<10 ? "0" : "")+ minutes;
	    String hoursStr = (hours<10 ? "0" : "")+ hours;
	    
		_lbluptime.setText(days + " days, " + hoursStr + "h " + minutesStr + "m " + secondsStr + "s");
	}
	
	public void setTimerIntervall(int intervall)
	{
		_txttimerintervall.setText(String.valueOf(intervall));
	}
	
	public int getTimerIntervall()
	{
		return Integer.valueOf(_txttimerintervall.getText());
	}
	
//	public void setSynchronicity(boolean synchron)
//	{
//		_cbsynchron.getCheckBox().setSelected(synchron);
//	}
	
	public void addActionListener(ActionListener l)
	{
		_butttime.addActionListener(l);
	}
	
//	public void addItemListener(ItemListener l)
//	{
//		_cbsynchron.getCheckBox().addItemListener(l);
//	}
	
	public void updateNetworkState(SnmpStatus status)
	{
		_lbldata.setText(String.valueOf(status.getDataCount()));
		_lblreceive.setText(String.valueOf(status.getReceivedPduCount()));
		_lblsend.setText(String.valueOf(status.getSendPduCount()));
		_lbltimedout.setText(String.valueOf(status.getTimedOutPduCount()));
	}
	
	private void initialize()
	{
		Dimension labelwidth = new Dimension(250, 20);
		this.setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
		this.setBorder(BorderFactory.createTitledBorder("Network Information and State"));
		
		_lblname = new JLabel();
		_lbluptime = new JLabeldComponent("Switch Up Time: ", JLabeldComponent.LABEL, labelwidth);
		_lbldata = new JLabeldComponent("Received Data Packets: ", JLabeldComponent.LABEL, labelwidth);
		_lblreceive = new JLabeldComponent("Received Packets: ", JLabeldComponent.LABEL, labelwidth);
		_lblsend = new JLabeldComponent("Sent Packets: ", JLabeldComponent.LABEL, labelwidth);
		_lbltimedout = new JLabeldComponent("Timed Out Packets: ", JLabeldComponent.LABEL, labelwidth);
		_txttimerintervall = new JMaskedTextField("SNMP Request Rate (milliseconds): ", JMaskedTextField.INTNUM, labelwidth);
		_txttimerintervall.setMin(10);
		//_cbsynchron = new JLabeldComponent("Process Synchron: ", JLabeldComponent.CHECKBOX, labelwidth);
		_butttime = new JButton("Apply New Request Rate");
		_butttime.setActionCommand(ResourceManager.getInstance().getActionTransfer());
		
		_butttest = new JButton("Query Signal");
		_butttest.addActionListener(this);
		
        JPanel buttontimepanel = new JPanel();
        buttontimepanel.setLayout(new BoxLayout(buttontimepanel, BoxLayout.LINE_AXIS));
        buttontimepanel.add(Box.createHorizontalGlue());
        buttontimepanel.add(_butttime);
        
        JPanel buttonquerypanel = new JPanel();
        buttonquerypanel.setLayout(new BoxLayout(buttonquerypanel, BoxLayout.LINE_AXIS));
        buttonquerypanel.add(Box.createHorizontalGlue());
        buttonquerypanel.add(_butttest);
		
		this.add(_lblname);
		this.add(Box.createRigidArea(ResourceManager.getInstance().getConfigurationLabelDimension()));
		this.add(_lbluptime);
		this.add(_lblsend);
		this.add(_lbldata);
		this.add(_lblreceive);
		this.add(_lbltimedout);
		this.add(Box.createRigidArea(ResourceManager.getInstance().getConfigurationLabelDimension()));
		//this.add(_cbsynchron);
		this.add(_txttimerintervall);
		this.add(buttontimepanel);
		this.add(buttonquerypanel);
	}

	public void actionPerformed(ActionEvent evt) 
	{
		new TestNetworkOptionPane(null, _sdm);
	}
}
